// checkExistGenerate.cpp: implementation of the CheckExistGenerate class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "phsshare.h"
#include "checkExistGenerate.h"
#include "Joint.h"
#include "JoinInfo.h"
#include "JointFunction.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC_CREATE(CheckExistGenerate, JointGeneratorArithmetic, JointGeneratorArithmetic::eFINDEXIST)

CheckExistGenerate::CheckExistGenerate()
{

}

CheckExistGenerate::~CheckExistGenerate()
{

}

BOOL CheckExistGenerate::Execute(Joint*& pJoint)
{
	JointFunction* pFunction = NULL;
	JointFunction* pTempFunction = NULL;

	pFunction = GetFirst().GetFirst();
	while(pFunction != NULL)
	{
		pJoint = pFunction->GetJoint();
		if(pJoint != NULL)
		{
			pTempFunction = pJoint->GetPrev();
			ASSERT(pTempFunction);

			if(pTempFunction->GetOwner() == GetNext().GetOwner())
			{
				return TRUE;
			}

			pTempFunction = pJoint->GetNext();
			ASSERT(pTempFunction);

			if(pTempFunction->GetOwner() == GetNext().GetOwner())
			{
				pJoint->Reverse();
				return TRUE;
			}
		}
		pFunction = GetFirst().GetNext();
	}

	return FALSE;
}